import random

isFlag = True
bank_dict = {"name": "中国农业银行的昌平沙河支行",
             "account": {"12345678": {"account": "12345678", "accountKind": 1, "password": 123456, "name": "张三",
                                      "address": {"country": "中国", "province": "河南", "street": "凤阳街", "door": "101"},
                                      "balance": 10000, "bankName": "中国农业银行的昌平沙河支行"}

                         }}
# 判断卡号是否存在
def account_existence(card_number):
    if card_number in bank_dict.get("account").keys():
        return True
    else:
        return False
#开户
def account_open():
    account =  str(random.randint(10000000,99999999))
    if len(bank_dict) >100:
        print("用户库存已到上限")
        return 3;
    if account_existence(account):
        print("该卡号已存在")
        return 2;
    else:
        add_dict = {"country": '', "province": '', "street": '', "door": '0'}
        name = input("请输入姓名：")
        password = 0
        accountKind = 0
        flag = True
        while flag:
            try:
                password = int(input("请输入密码："))
                if len(str(password)) != 6:
                    print("密码必须为6位数字")
                else:
                    flag = False
            except ValueError:
                print("请输入六位数字")
                continue
        print("请输入如地址")
        flag = True
        while flag:
            print("请输入账户类型，1类，2类")
            try:
               accountKind = int(input())
               if accountKind != 1 and accountKind != 2:
                   print("请输入1，或2")
               else:
                   flag = False

            except ValueError:
                print("请输入数字1，或者2")

        print("请输入国家：")
        country = input()
        add_dict.update({"country": country})
        print("请输入省份：")
        country = input()
        add_dict.update({"province": country})
        print("请输入街道：")
        country = input()
        add_dict.update({"street": country})
        print("请输入门牌号：")
        country = input()
        add_dict.update({"door": country})

        bank_dict.get("account").update({account:{"account":account,"accountKind":accountKind,"name":name,
                                         "password":password,"address":add_dict,"balance":0,"bankName":bank_dict.get("name")}})
        return 1

#存钱
def account_deposit():
    print("请输入卡号")
    card_number = input()
    if account_existence(card_number):
        print("请输入存款金额")
        try:
            money = int(input())
            if money < 0:
                print("存款金额不能为负数")
            else:
                bank_dict.get("account").get(card_number).update({"balance":bank_dict.get("account").get(card_number).get("balance")+money})
                return True

        except ValueError:
            print("请输入数字")
    else:
        return False
#取钱
def account_withdraw():
    print("请输入卡号")
    card_number = input()
    if account_existence(card_number):
        print("请输入卡密码")

        password = int(input())
        if password != bank_dict.get("account").get(card_number).get("password"):
            return 2
        print("请输入取款金额")
        try:
            money = int(input())
            if money < 0:
                print("取款金额不能为负数")
            elif money > bank_dict.get("account").get(card_number).get("balance"):
                return 3
            else:
                bank_dict.get("account").get(card_number).update({"balance":bank_dict.get("account").get(card_number).get("balance")-money})
                return 1

        except ValueError:
            print("请输入数字")
    else:
        return False
#转账
def account_transfer():
    print("请输入转出卡号")
    out_card_number = input()
    if account_existence(out_card_number):
        print("请输入转入卡号")
        in_card_number = input()
        if account_existence(in_card_number):
            print("请输入转出卡密码")
            out_password = int(input())
            if out_password != bank_dict.get("account").get(out_card_number).get("password"):
                return 2
            print("请输入转账金额")
            limit = 50000 if bank_dict.get("account").get(out_card_number).get("accountKind") == 1 else 20000

            try:
                money = int(input())
                if money < 0 and money > limit:
                    print("转账金额不能为负数,或转账金额超过账户限额")
                elif money > bank_dict.get("account").get(out_card_number).get("balance"):
                    return 3
                else:
                    bank_dict.get("account").get(out_card_number).update({"balance":bank_dict.get("account").get(out_card_number).get("balance")-money})
                    bank_dict.get("account").get(in_card_number).update({"balance":bank_dict.get("account").get(in_card_number).get("balance")+money})
                    return 0

            except ValueError:
                print("请输入数字")
        else:
            return 1

    else:
        return 1
#查询
def account_query():
    print("请输入卡号")
    card_number = input()
    if account_existence(card_number):
        print("请输入密码")
        password = int(input())
        if password != bank_dict.get("account").get(card_number).get("password"):
            print("密码错误")
            return
        print("当前账号：",card_number)
        print("姓名：",bank_dict.get("account").get(card_number).get("name"))
        print("账户类型：",bank_dict.get("account").get(card_number).get("accountKind"))
        print("密码：",bank_dict.get("account").get(card_number).get("password"))
        print("账户余额：",bank_dict.get("account").get(card_number).get("balance"))
        print("当前账户开户银行：",bank_dict.get("account").get(card_number).get("bankName"))
        print("地址：",bank_dict.get("account").get(card_number).get("address"))
    else:
        print("卡号不存在")
while isFlag:
    print("********************************")
    print("*" + "     中国农业银行账户管理系统     " + "*")
    print("********************************")
    print("*            选项               *")
    print("*           1.开户              *")
    print("*           2.存钱              *")
    print("*           3.取钱              *")
    print("*           4.转账              *")
    print("*           5.查询              *")
    print("*           6.Bye              *")
    print("********************************")
    print("请输入操作种类")
    choice = int(input())
    try:
        if choice == 1:
            print("这是开户选项:")
            is_success = account_open()
            if is_success == 1:
                print("开户成功")
                account_number = list(bank_dict.get("account").keys())[-1]
                print("您的卡号为：",account_number)
            elif is_success == 2:
                print("该卡号已存在")
            elif is_success == 3:
                print("用户库存已到上限")
            else:
                print("开户失败")
        elif choice == 2:
            print("这是存钱选项")
            is_success = account_deposit()
            if is_success:
                print("存款成功")
            else:
                print("卡号不存在")
        elif choice == 3:
            print("这是取钱选项")
            is_success = account_withdraw()
            if is_success == 1:
                print("取款成功")
            elif is_success == 2:
                print("密码错误")
            elif is_success == 3:
                print("余额不足")
            else:
                print("卡号不存在")
        elif choice == 4:
            print("这是转账选项")
            is_success = account_transfer()
            if is_success == 0:
                print("转账成功")
            elif is_success == 1:
                print("卡号不存在")
            elif is_success == 2:
                print("密码错误")
            elif is_success == 3:
                print("余额不足")
        elif choice == 5:
            print("这是查询选项")
            account_query()
        elif choice == 6:
            print("系统关闭")
            break
        else:
            print("请输入正确的操作选项")

    except ValueError:
        print("请输入正确的数字选择")
        continue
